#!/bin/sh

# proxyport=1984

if [ "$1x" != "connectx" ]; then

if [ -z $1 ]; then
	echo "Usage: $0 [username@]host[:port]" 2>&1
	exit 0
fi

# echo -n "Password: "

# stty -echo; read pass; stty echo; echo ""

expect -- << EOF
spawn $0 connect $1

while 1 {
	expect {
		"Password:" {
			send -- "$pass\r"
		}
		"password:" {
			send -- "$pass\r"
		}
		"yes/no" {
			send -- "yes\r"
		}
		"id_rsa" {
			send -- "\r"
		}
		"Permission denied" {
			puts -- "Password error."
			break
		}
	}
}
EOF
else
	shift
	# python freessh.py
	tempport=`echo $1 | perl -nle 'print /\:(\d+)$/ ? $1 : "22"'`
	while [ 1 ]
	do
		ssh -v -p $port -CN -D localhost:$proxyport $1
		while netcat -zv localhost $proxyport
			do sleep 5
		done
		sleep 5
	done
fi
